12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import NextAuth from "next-auth";
- import CredentialsProvider from "next-auth/providers/credentials";
- import * as R from "ramda";
- const authOptions = {
- // https://generate-secret.vercel.app/32
- secret: process.env.SECRET,
- // Configure one or more authentication providers
- providers: [
- CredentialsProvider({
- // The name to display on the sign in form (e.g. 'Sign in with...')
- name: "Cocorobo cloud",
- // The credentials is used to generate a suitable form on the sign in page.
- // You can specify whatever fields you are expecting to be submitted.
- // e.g. domain, username, password, 2FA token, etc.
- // You can pass any HTML attribute to the <input> tag through the object.
- credentials: {
- userId: { label: "theUserId", type: "text", required: true },
- // loginUsername: { label: "用户名", type: "text" },
- // loginPassword: { label: "密码", type: "password" },
- },
- async authorize(credentials, req) {
- return { id: credentials.userId, name: 'anonymous' };
- // You need to provide your own logic here that takes the credentials
- // submitted and returns either a object representing a user or value
- // that is false/null if the credentials are invalid.
- // e.g. return { id: 1, name: 'J Smith', email: 'jsmith@example.com' }
- // You can also use the `req` object to obtain additional parameters
- // (i.e., the request IP address)
- /*
- const res = await fetch("https://beta.api.cocorobo.cn/api/user", {
- method: "POST",
- body: JSON.stringify(
- R.pick(["loginUsername", "loginPassword"], credentials)
- ),
- headers: {
- "Content-Type": "application/json",
- Origin: "https://edu.cocorobo.cn",
- },
- });
- if (res.status !== 200) {
- return null;
- }
- const resJson = await res.json();
- const user = resJson?.[0]?.[0];
- // If no error and we have user data, return it
- if (res.ok && user && user.active) {
- return { ...user, id: user.userid, name: user.username };
- }
- */
- },
- }),
- ],
- callbacks: {
- // we have no db intergrate, `user` is always empty because there is no db record
- async session({ session, token, user: _user }) {
- // Send properties to the client, like an access_token from a provider.
- session.user.id = token.sub
- try {
- const res = await fetch(
- `https://pbl.cocorobo.cn/api/pbl/selectUser?userid=${token.sub}`,
- {
- method: "GET",
- headers: {
- "Content-Type": "application/json",
- },
- }
- );
- const username = (await res.json())?.[0]?.[0]?.username;
- session.user.name = username;
- } catch (e) {
- session.user.name = token.name
- }
- return session;
- },
- },
- };
- const handler = NextAuth(authOptions);
- export { handler as GET, handler as POST };
|